{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图像的读取与显示"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 图像读取"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 图像读取入口参数：\n",
    "  - cv2.IMREAD_COLOR：彩色图像\n",
    "  - cv2.IMREAD_GRAYSCALE：灰度图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 #opencv的缩写为cv2\n",
    "import matplotlib.pyplot as plt # matplotlib库用于绘图展示\n",
    "import numpy as np   # numpy数值计算工具包\n",
    "\n",
    "# 魔法指令，直接展示图，Jupyter notebook 特有\n",
    "%matplotlib inline   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[142, 151, 160],\n",
       "        [146, 155, 164],\n",
       "        [151, 160, 169],\n",
       "        ...,\n",
       "        [156, 172, 185],\n",
       "        [155, 171, 184],\n",
       "        [154, 170, 183]],\n",
       "\n",
       "       [[107, 118, 126],\n",
       "        [112, 123, 131],\n",
       "        [117, 128, 136],\n",
       "        ...,\n",
       "        [155, 171, 184],\n",
       "        [154, 170, 183],\n",
       "        [153, 169, 182]],\n",
       "\n",
       "       [[108, 119, 127],\n",
       "        [112, 123, 131],\n",
       "        [118, 129, 137],\n",
       "        ...,\n",
       "        [154, 170, 183],\n",
       "        [153, 169, 182],\n",
       "        [152, 168, 181]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[162, 186, 198],\n",
       "        [157, 181, 193],\n",
       "        [142, 166, 178],\n",
       "        ...,\n",
       "        [181, 204, 206],\n",
       "        [170, 193, 195],\n",
       "        [149, 172, 174]],\n",
       "\n",
       "       [[140, 164, 176],\n",
       "        [147, 171, 183],\n",
       "        [139, 163, 175],\n",
       "        ...,\n",
       "        [167, 187, 188],\n",
       "        [123, 143, 144],\n",
       "        [104, 124, 125]],\n",
       "\n",
       "       [[154, 178, 190],\n",
       "        [154, 178, 190],\n",
       "        [121, 145, 157],\n",
       "        ...,\n",
       "        [185, 198, 200],\n",
       "        [130, 143, 145],\n",
       "        [129, 142, 144]]], dtype=uint8)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img = cv2.imread('01_Picture/01_cat.jpg') \n",
    "print(type(img)) # img 的类型为 numpy.ndarray 类型\n",
    "img              # uint8 的取值范围在 0-255 之间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 图像显示"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 图像显示(普通方法)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# opencv 默认读取格式是 BGR 格式，matplotlib 或其他库的读取格式可能是 RGB 的\n",
    "# opencv 读取并用 opencv 自带的展示函数不需要进行通道转换，但 opencv 读取后用其他库展示图片需要通道转换    \n",
    "\n",
    "# 图像显示时,可以创建多个窗口\n",
    "\n",
    "# 第一个入口参数为展示图像窗口的名字\n",
    "# 第二个入口参数为展示图像窗口中所展示的图像\n",
    "img = cv2.imread('01_Picture/01_cat.jpg') \n",
    "cv2.imshow('image_cat',img)  \n",
    "\n",
    "# 等待时间，毫秒级，0表示任意键终止，5000ms表示5s\n",
    "cv2.waitKey(5000)  \n",
    "\n",
    "# 销毁图像窗口\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 图像显示(函数方法)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘图显示(封装函数)\n",
    "def cv_show(name,img):\n",
    "    cv2.imshow(name,img)\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()\n",
    "    \n",
    "cv_show('image_cat',img)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
